package com.visionork.android.twe.activity;

import java.util.ArrayList;
import java.util.List;

import com.visionork.android.twe.model.Episode;
import com.visionork.rss.FeedParser;
import com.visionork.rss.FeedParserFactory;
import com.visionork.rss.Message;
import com.visionork.rss.ParserType;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class WhatsNewActivity extends ListActivity {
	private final static String FEEDURL = "http://feeds.feedburner.com/twsugoideas?format=xml";
	private List<Message> messages;
	private ArrayList<Episode> episodes;
	private class LoadFeedTask extends AsyncTask<Void, Void, Void> {
		private ProgressDialog pd;
		

		@Override
		protected Void doInBackground(Void... params) {
			episodes = new ArrayList<Episode>();
			FeedParser parser = FeedParserFactory.getParser(
					ParserType.ANDROID_SAX, FEEDURL);
			messages = parser.parse();
			return null;
		}

		@Override
		protected void onPreExecute() {
			Resources res = getResources();
			pd = ProgressDialog.show(WhatsNewActivity.this,
					res.getString(R.string.d_loading),
					res.getString(R.string.d_loading_detail), true, false);
		}

		@Override
		protected void onPostExecute(Void param) {
			List<String> titles = new ArrayList<String>(messages.size());
			for (Message msg : messages) {
				Episode episode = new Episode();
				episode.setTitle(msg.getTitle());
				episode.setUrlLink(msg.getLink().toExternalForm());
				episodes.add(episode);
				titles.add(msg.getTitle());
			}

			setListAdapter(new ArrayAdapter<String>(WhatsNewActivity.this,
					R.layout.episode_list_item, titles));

			pd.dismiss();
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.episode_list);
		loadFeed();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.program_list_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected (MenuItem item) {
		switch (item.getItemId()) {
		case R.id.refresh:
			loadFeed();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Episode episode = episodes.get(position);
		Intent intent = new Intent(this, EpisodeActivity.class);
		intent.putExtra(EpisodeActivity.DYNAMIC, true);
		intent.putExtra(EpisodeActivity.URLSTRING, episode.getUrlLink());
		startActivity(intent);
	}
	
	private void loadFeed() {
		LoadFeedTask task = new LoadFeedTask();
		task.execute(new Void[] {});
	}
}
